@ECHO OFF

rem ===========================================================================
rem TESTED ON: zlib-1.2.5
rem REQUIRES: cygwin: find, sed, cp
rem           visual studio
rem PATH: find, sed, cp
rem HOWTO: win32\DLL_FAQ.txt, contrib\vstudio\readme.txt
rem ===========================================================================

rem =====SETTINGS==============================================================

rem Library architecture, possible values: Win32, x64
set ARCH=Win32

rem Build debug or release version, possible values: Debug, Release
set BUILD_TYPE=Debug

rem Library type, possible values: Static, Dynamic
set LIBRARY_TYPE=Static

rem Visual C++ version, possible values: 8, 9, 10
set VC_VERSION=10

rem Visual Studio path
set VC_PATH=C:\Program Files (x86)\Microsoft Visual Studio 10.0

rem Path to .bat utils, for example vs9tovs8.bat
set UTILS_PATH=%CD%

rem Zlib sources path
set ZLIB_SRC_PATH=%CD%\src\zlib-1.2.5

rem Directory where zlib will be installed
set INSTALL_ROOT=%CD%

rem Path to return after compilation
set RETURN_PATH=%CD%

rem =============================================================================

set INSTALL_PATH=%INSTALL_ROOT%\vc_%VC_VERSION%\%ARCH%\%BUILD_TYPE%\%LIBRARY_TYPE%\zlib


rem http://msdn.microsoft.com/en-us/library/x4d2c09s(v=VS.80).aspx
if %ARCH% == Win32 (
    call "%VC_PATH%\VC\bin\vcvars32.bat"
) else (
    call "%VC_PATH%\VC\vcvarsall.bat" x86_amd64
)


cd "%ZLIB_SRC_PATH%\contrib\vstudio"


if %VC_VERSION% == 8 (
    rmdir /Q /S vc8
    mkdir vc8
    cp -rfd vc9/* vc8/
    call "%UTILS_PATH%\vs9tovs8.bat"
    set PROJECT_DIR=%CD%\vc8
    set PROJECT_EXT=vcproj
) else (
    if %VC_VERSION% == 9 (
        set PROJECT_DIR=%CD%\vc9
        set PROJECT_EXT=vcproj
    ) else (
        set PROJECT_DIR=%CD%\vc10
        set PROJECT_EXT=vcxproj
    )
)


rem 1) no winapi, use cdecl
rem 2) result names without wapi suffix
rem 3) use dynamic mt runtime, because static causes linker errors when compiling application with different runtimes
rem 4) use dynamic debug mt runtime
cd "%PROJECT_DIR%"

sed -b -i "s/ZLIB_WINAPI//" zlibvc.%PROJECT_EXT% zlibstat.%PROJECT_EXT%
sed -b -i "s/wapi\././" zlibvc.%PROJECT_EXT% zlibstat.%PROJECT_EXT%

if %VC_VERSION% == 10 (
    sed -b -i "s/<RuntimeLibrary>MultiThreaded<\/RuntimeLibrary>/<RuntimeLibrary>MultiThreadedDLL<\/RuntimeLibrary>/" zlibvc.%PROJECT_EXT% zlibstat.%PROJECT_EXT%
    sed -b -i "s/<RuntimeLibrary>MultiThreadedDebug<\/RuntimeLibrary>/<RuntimeLibrary>MultiThreadedDebugDLL<\/RuntimeLibrary>/" zlibvc.%PROJECT_EXT% zlibstat.%PROJECT_EXT%
) else (
    sed -b -i "s/RuntimeLibrary=\"0\"/RuntimeLibrary=\"2\"/" zlibvc.%PROJECT_EXT% zlibstat.%PROJECT_EXT%
    sed -b -i "s/RuntimeLibrary=\"1\"/RuntimeLibrary=\"3\"/" zlibvc.%PROJECT_EXT% zlibstat.%PROJECT_EXT%
)

rem sed -b -i "s/AdditionalDependencies=.*masm.*$//" zzlibvc.%PROJECT_EXT% zlibstat.%PROJECT_EXT%
rem sed -b -i "s/ASMV;ASMINF;//" zlibvc.%PROJECT_EXT% zlibstat.%PROJECT_EXT%



rem this asm-inliners can be disabled by using "ReleaseWithoutAsm|Win32" vcbuild configuration
if %ARCH% == Win32 (
    cd ..\..\masmx86
    call bld_ml32.bat
) else (
    cd ..\..\masmx64
    call bld_ml64.bat
)


if %VC_VERSION% == 10 (
    set BUILD_COMMAND=msbuild
    set BUILD_CONFIGURATION=/t:rebuild /p:Configuration=%BUILD_TYPE%;Platform=%ARCH%
) else (
    set BUILD_COMMAND=vcbuild /rebuild
    set BUILD_CONFIGURATION="%BUILD_TYPE%|%ARCH%"
)


if %ARCH% == Win32 (
    set BUILD_ROOT=x86
) else (
    set BUILD_ROOT=x64
)

cd "%PROJECT_DIR%"
if %LIBRARY_TYPE% == Static (
    %BUILD_COMMAND% zlibstat.%PROJECT_EXT% %BUILD_CONFIGURATION%
    cd "%BUILD_ROOT%\ZlibStat%BUILD_TYPE%"
    echo d | xcopy *.lib "%INSTALL_PATH%\lib" /y
) else (
    %BUILD_COMMAND% zlibvc.%PROJECT_EXT% %BUILD_CONFIGURATION%

    cd "%BUILD_ROOT%\ZlibDll%BUILD_TYPE%"
    echo d | xcopy *.exp "%INSTALL_PATH%\lib" /y
    echo d | xcopy *.lib "%INSTALL_PATH%\lib" /y
    echo d | xcopy *.dll "%INSTALL_PATH%\bin" /y
)

echo f | xcopy ..\..\..\..\..\zconf.h "%INSTALL_PATH%\include\zlib\zconf.h" /y
echo f | xcopy ..\..\..\..\..\zlib.h "%INSTALL_PATH%\include\zlib\zlib.h" /y


cd "%RETURN_PATH%"

@ECHO ON
